from oasys.user import bp
from oasys.user.models import Deptment, Position, Employee, Attendance, Permission

from flask_restful import Resource
from oasys.user import api
from flask import jsonify, request


# 部门
class DeptmentResources(Resource):
    # 添加部门
    def post(self):
        # 获取前端提交的数据
        # username = request.form.get('username')
        # pwd = request.form.get('password')
        # 保存数据库
        # emp_obj = Employee()
        # emp_obj.name = username
        # emp_obj.password = pwd
        # emp_obj.save()
        name = request.form.get('name')
        desc = request.form.get('desc')
        dept_obj = Deptment()
        dept_obj.name = name
        dept_obj.desc = desc
        dept_obj.save()
        # 响应
        return jsonify({'code': '200',
                        'msg': 'success',
                        'data': {'id': dept_obj.id, 'name': dept_obj.name, 'desc': dept_obj.desc}})

    # 展示和查看操作
    def get(self):
        # 获取前端传过来的id 根据id朝招部门  如果能查找到就显示当前部门的信息
        id = request.args.get('id')
        if id:
            dept_obj = Deptment.query.get(id)
            return jsonify(
                {'code': 200,
                 'msg': 'success',
                 'data': {'id': dept_obj.id, 'name': dept_obj.name, 'desc': dept_obj.desc}})
        else:
            # 查询所有的部门
            dept_obj_list = Deptment.query.all()
            new_list = []
            # 遍历所有的部门，将部门的信息放在一个字典，然后把字典放在一个列表中 传给前台
            for dept_obj in dept_obj_list:
                dic = {}
                dic['id'] = dept_obj.id
                dic['name'] = dept_obj.name
                dic['desc'] = dept_obj.desc
                new_list.append(dic)
            return jsonify({'code': 200, 'msg': 'ok', 'data': new_list})

    # 修改
    def put(self):
        id = request.form.get('id')
        name = request.form.get('name')
        desc = request.form.get('desc')
        dept_obj = Deptment.query.get(id)
        dept_obj.name = name
        dept_obj.desc = desc
        dept_obj.save()
        dic = {
            'code': 200,
            'msg': 'sucsess',
            'data': {

                'name': dept_obj.name,
                'desc': dept_obj.desc,
            }
        }
        return jsonify(dic)

    def delete(self):
        id = request.args.get('id')
        dept_obj = Deptment.query.get(id)
        dept_obj.delete()
        return jsonify({'msg': 'success'})


# 职位
class PositionResources(Resource):
    # 获取和编辑职位
    def get(self):
        # 根据职位id来获取职位 如果id存在就展示信息
        position_id = request.args.get('position_id')
        if position_id:
            position_obj = Position.query.get(position_id)
            dic = {'msg': 'ok',
                   'data': {
                       'id': position_obj.id,
                       'name': position_obj.name,
                       'level': position_obj.level
                   }
                   }
            return jsonify(dic)
        else:
            # 展示部门下的职位 获取到比阿尼 取出 禅道前端
            dept_id = request.args.get('dept_id')
            position_obj_list = Position.query.filter(Position.deptment_id == dept_id).all()
            new_list = []
            for position_obj in position_obj_list:
                dic = {}
                dic['id'] = position_obj.id
                dic['name'] = position_obj.name
                dic['level'] = position_obj.level
                dic[dept_id] = position_obj.deptment_id
                new_list.append(dic)
            dic = {
                'code': 200,
                'msg': 'ok',
                'data': new_list
            }
            return jsonify(dic)

    # 添加职位
    def post(self):
        dept_id = request.form.get('dept_id')
        name = request.form.get('name')
        level = request.form.get('level')
        position_obj = Position()
        position_obj.name = name
        position_obj.level = level
        position_obj.deptment_id = dept_id
        position_obj.save()
        return jsonify({'msg': 'ok'})

    # 提交
    def put(self):
        # 修改职位信息  先传出来职位的id 然后展示修改
        # position_obj = Position()
        id = request.form.get('id')
        name = request.form.get('name')
        level = request.form.get('level')
        position_obj = Position.query.get(id)
        position_obj.name = name
        position_obj.level = level
        position_obj.update()
        return jsonify({'msg': 'success'})

    # 删除
    def delete(self):
        id = request.form.get('id')
        position_obj = Position.query.get(id)
        position_obj.delete()
        dic = {
            'msg': 'ok',
        }
        return jsonify(dic)


# 员工
class EmplpyeeResources(Resource):
    # 获取单个和所有员工
    def get(self):
        id = request.args.get('id')
        if id:
            emp_obj = Employee.query.get(id)
            position_obj = emp_obj.position
            dept_obj = position_obj.dept
            dic = {
                'data': {
                    'id': emp_obj.id,
                    'name': emp_obj.username,
                    'dept_name': dept_obj.name,
                    'position_name': position_obj.name
                },
                'code': 200,
                'msg': 'ok'
            }

            return jsonify(dic)
        else:
            emp_obj_list = Employee.query.all()
            print(emp_obj_list)
            # 展示所有员工的所属部门和职位
            new_list = []
            for emp_obj in emp_obj_list:
                dic = {}
                dic['id'] = emp_obj.id
                dic['name'] = emp_obj.username
                position_obj = emp_obj.position
                dept_obj = position_obj.dept
                dic['dept_name'] = dept_obj.name
                dic['position_name'] = position_obj.name
                new_list.append(dic)
            dic = {
                'code': 200,
                'msg': 'ok',
                'data': new_list
            }
            return jsonify(dic)

    def post(self):
        pass

    # 删除员工
    def delete(self):
        id = request.args.get('id')
        emp_obj = Employee.query.get(id)
        emp_obj.delete()
        return jsonify({'msg': 'ok'})


# 添加员工  添加员工时，因为要显示员工的职位和部门  信息要展示着在选择关系上 select option
class EmployeeAddResources(Resource):
    def get(self):
        id = request.args.get('id')
        if id:  # 编辑室液要把所有的类型传给前台来吃力这些部门类型职位的拼接
            employee_obj = Employee.query.get(id)
            position_obj_list = Position.query.all()
            name_list = []
            for position_obj in position_obj_list:
                dic = {}
                dept_obj = position_obj.dept
                dic['name'] = dept_obj.name + position_obj.name
                dic['position_id'] = position_obj.id
                name_list.append(dic)
            dic = {
                'username': employee_obj.username,
                'password': employee_obj.password,
                'position_id': employee_obj.position_id,
                'data': name_list
            }
            return jsonify(dic)

        else:  # 添加时查看所有职位
            position_obj_list = Position.query.all()
            new_list = []
            for position_obj in position_obj_list:
                dept_obj = position_obj.dept  # 反向获取部门
                dic = {}
                dic['name'] = dept_obj.name + position_obj.name
                dic['position_id'] = position_obj.id
                new_list.append(dic)
            dic = {
                'code': 200,
                'msg': 'success',
                'data': new_list
            }
            return jsonify(dic)

    def post(self):
        position_id = request.form.get('position_id')
        name = request.form.get('username')
        pwd = request.form.get('password')

        employee_obj = Employee()
        employee_obj.username = name
        employee_obj.password = pwd
        employee_obj.position_id = position_id
        employee_obj.save()
        dic = {
            'code': 200,
            'msg': 'success',
        }
        return jsonify(dic)

    # 保存提交
    def put(self):
        id = request.form.get('id')
        username = request.form.get('username')
        password = request.form.get('password')
        position_id = request.form.get('position_id')
        emp_obj = Employee.query.get(id)
        emp_obj.username = username
        emp_obj.password = password
        emp_obj.position_id = position_id
        emp_obj.update()
        dic = {
            'code': 200,
            'msg': 'ok'
        }
        return jsonify(dic)


# 个人考勤
class AttendanceResources(Resource):
    # 获取 2好员工的出勤
    def get(self):
        employee_id = 2
        att_obj_list = Attendance.query.filter(Attendance.employee_id == employee_id).all()
        new_list = []
        for att_obj in att_obj_list:
            dic = {}
            dic['id'] = att_obj.id
            dic['atype'] = att_obj.atype
            dic['areason'] = att_obj.areason
            dic['astatus'] = att_obj.astatus
            dic['starttime'] = att_obj.starttime
            dic['endtime'] = att_obj.endtime
            dic['checkman'] = att_obj.checkman
            new_list.append(dic)
        result = {
            'code': 200,
            'msg': 'success',
            'data': new_list
        }
        return jsonify(result)

    def post(self):
        atten_obj = Attendance()
        atype = request.form.get('atype')
        areason = request.form.get('areason')
        starttime = request.form.get('starttime')
        adate = request.form.get('adate')
        endtime = request.form.get('endtime')
        atten_obj.atype = atype
        atten_obj.areason = areason
        atten_obj.starttime = starttime
        atten_obj.endtime = endtime
        atten_obj.adate = adate
        atten_obj.employee_id = 2
        atten_obj.save()
        dic = {
            'code': 200,
            'msg': 'success'
        }
        return jsonify(dic)

    def delete(self):
        pass

    def put(self):
        pass


# 下属考勤
class SubAttendanceResources(Resource):

    def get(self):
        # 下属考勤 2号下属
        # 首先，获取2号下属的职位  潘端职位等级中 比该2号的回味等级小员工 获取到员工后，在进行职位和
        #  根据等级比他小的职位来获取员工 然后根据员工来获取 对应的出勤  获取对应部门下的职位  两个条件 and
        employee_id = 2
        emp_obj = Employee.query.get(employee_id)
        pos_obj = emp_obj.position
        level = pos_obj.level
        dept_obj = pos_obj.dept
        dept_id = dept_obj.id
        pos_obj_list = Position.query.filter(Position.level < level, Position.deptment_id == dept_id).all()
        print(pos_obj_list)

        news_list = []
        for pos_obj in pos_obj_list:
            emp_obj_list = pos_obj.employees
            print(emp_obj_list)
            for emps_obj in emp_obj_list:
                empDic = {}  # 用户名和假条 返回给前端的数据格式是名字 加假条 先获取名字
                empDic['name'] = emps_obj.username
                att_obj_list = emps_obj.attendances
                print(att_obj_list)
                new_list = []
                for att_obj in att_obj_list:
                    dic = {}
                    dic['id'] = att_obj.id
                    dic['atype'] = att_obj.atype
                    dic['astatus'] = att_obj.astatus
                    dic['areason'] = att_obj.areason
                    dic['date'] = att_obj.adate
                    dic['checkman'] = att_obj.checkman
                    dic['starttime'] = att_obj.starttime
                    dic['endtime'] = att_obj.endtime
                    new_list.append(dic)
                empDic['new_list'] = new_list
                news_list.append(empDic)
        results = {
            'code': 200,
            'data': news_list,
            'msg': 'success'
        }
        return jsonify(results)

    def post(self):
        pass

    def put(self):
        employee_id = 2
        id = request.form.get('id')
        print(id)
        flag = request.form.get('flag')
        att_obj = Attendance.query.get(id)
        print(att_obj)
        if flag == '1':
            att_obj.astatus = '驳回'
        else:
            att_obj.astatus = '通过'
        emp_obj = Employee.query.get(employee_id)
        # 这个审核人就是当前用户本身
        att_obj.checkman = emp_obj.username
        att_obj.update()
        dic = {
            'code': 200,
            'msg': 'success'
        }
        return jsonify(dic)


class PermissionResources(Resource):
    def post(self):
        name = request.form.get('name')
        desc = request.form.get('desc')
        perm_obj = Permission()
        perm_obj.name = name
        perm_obj.desc = desc
        perm_obj.save()
        dic = {
            'code': 200,
            'msg': 'success'
        }
        return jsonify(dic)

    def get(self):
        # 传递一个id 权限的 如果存在就展示  没有传就展示全部权限
        id = request.args.get('id')
        if id:
            perm_obj = Permission.query.get(id)
            per_info = {
                'id': perm_obj.id,
                'name': perm_obj.name,
                'desc': perm_obj.desc
            }
            dic = {
                'code': 200,
                'msg': 'ok',
                'data': per_info
            }
            return jsonify(dic)
        else:

            perm_obj_list = Permission.query.all()
            new_list = []
            for perm_obj in perm_obj_list:
                dic = {}
                dic['name'] = perm_obj.name
                dic['desc'] = perm_obj.desc
                new_list.append(dic)
            dic = {
                'code': 200,
                'data': new_list,
                'msg': 'ok'
            }
            return jsonify(dic)

    def put(self):
        id = request.form.get('id')
        name = request.form.get('name')
        desc = request.form.get('desc')
        perm_obj = Permission.query.get(id)
        perm_obj.name = name
        perm_obj.desc = desc
        perm_obj.update()

    def delete(self):
        id = request.args.get('id')
        perm_obj = Permission.query.get(id)
        perm_obj.delete()
        dic = {
            'code': 200,
            'msg': 'ok'
        }
        return jsonify(dic)


# 权限和职位关联的关系


class PermissionPositionResources(Resource):
    # 获取上所有的权限 然后 和有关联的id  全部帅哥给前端
    def get(self):
        perm_id = request.args.get('perm_id')
        perm_obj = Permission.query.get(perm_id)
        print(perm_obj)
        pos_obj_list = perm_obj.positions  # 获取职位
        # 获取选中的  和 所有的 职位和部门 信息
        a_list = []
        all_pos_obj_list = Position.query.all()
        for pos_obj in pos_obj_list:
            a_list.append(pos_obj.id)
        all_list = []
        for pos_obj in all_pos_obj_list:
            dic = {
                'id': pos_obj.id,
                'name': pos_obj.name,
                'deptName': pos_obj.dept.name
            }
            all_list.append(dic)
        dic = {
            'code': 200,
            'msg': 'success',
            'alreadyPosIdList': a_list,
            'allPosInfoList': all_list
        }

        return jsonify(dic)

        pass

    # 类比于 购物车功能
    def put(self):
        perm_id = request.args.get('perm_id')
        # 选中的id  原先关联的职位信息
        pos_id_list = request.args.getlist('pos_ids')  # 获取选中的关联权限的职位
        # 生成的是一个列表 列表里面放着对象的id
        perm_obj = Permission.query.get(perm_id)
        pos_obj_list = []  # yoga一个列表来接收这些对象
        for pos_id in pos_id_list:
            pos_obj = Position.query.get(pos_id)  # 根据获取到的id去查找这些对象
            pos_obj_list.append(pos_obj)  # 把对象放入列表
        perm_obj.positions = pos_obj_list  # 权限下关联的职位信息更新为选中的id的生成的对象
        perm_obj.update()
        dic = {
            'code': 200,
            'msg': 'ok'
        }
        return jsonify(dic)


# 前端控制显示

class ShowPermissionResources(Resource):
    def get(self):
        #获取一个对象的信息 查询到所欲的关联对象 和他本身关联的随想
        id = 2
        emp_obj = Employee.query.get(2)
        pos_obj = emp_obj.position
        perm_obj_list = pos_obj.permissions
        all_perm_obj_list = Permission.query.all()
        already_perm_id_list = []
        for perm_obj in perm_obj_list:
            already_perm_id_list.append(perm_obj.id)
        new_list = []
        for perm_obj in all_perm_obj_list:
            dic = {}
            dic['id'] = perm_obj.id
            dic['name'] = perm_obj.name
            new_list.append(dic)
        results = {
            'code': 200,
            'msg': 'ok',
            'alreadyPid': already_perm_id_list,
            'allPList': new_list
        }
        return jsonify(results)

# 找出所有的部门和閇所对应职位所对应的员工 ， 取出他们长度的和来表示数量
class IndexResources(Resource):
    def get(self):
        dept_obj_list = Deptment.query.all()
        print(dept_obj_list)
        new_list = []
        for dept_obj in dept_obj_list:
            dic = {}
            dic['deptName'] = dept_obj.name
            length = 0
            print(dic)
            pos_obj_list = dept_obj.positions

            for pos_obj in pos_obj_list:
                emp_obj_list = pos_obj.employees
                print(emp_obj_list)
                length += len(emp_obj_list)
            dic['empNum'] = length  # 卡住了
            print(dic)
            new_list.append(dic)
        print(pos_obj_list)
        results = {
            'code': 200,
            'msg': 'ok',
            'data': new_list
        }
        return jsonify(results)


# 添加路由
api.add_resource(IndexResources, '/index/')
api.add_resource(DeptmentResources, '/deptment/')
api.add_resource(PositionResources, '/position/')
api.add_resource(EmplpyeeResources, '/employee/')
api.add_resource(EmployeeAddResources, '/employee2/')
api.add_resource(AttendanceResources, '/attendance/')
api.add_resource(SubAttendanceResources, '/subattendance/')
api.add_resource(PermissionResources, '/permission/')
api.add_resource(PermissionPositionResources, '/ppr/')
api.add_resource(ShowPermissionResources, '/spr/')
